package com.xhdx.modules.jbpm4.tool;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.jbpm.api.HistoryService;
import org.jbpm.api.history.HistoryActivityInstance;
import org.jbpm.api.history.HistoryProcessInstance;
import org.jbpm.api.history.HistoryProcessInstanceQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * <p>className: JbpmHistoryTaskTool</p>
 * <p>description: 工作流历史服务查询工具类</p>
 *
 * @author xhdx
 * @version 1.0.0
 * @date 2020-05-25 22:00
 */

@Slf4j
@Data
@Component
public class JbpmHistoryTaskTool {

    /**
     * TaskService-流程历史务接口
     */
    @Autowired
    @Qualifier("historyService")
    private HistoryService historyService;


    /***
     * 查询某一特定流程定义的所有历史流程实例
     *
     * @author
     * @date 2020/5/25 22:09
     * @param processDefinitionId 流程定义ID
     * @return java.util.List<org.jbpm.api.history.HistoryProcessInstance>
     */
    public List<HistoryProcessInstance> queryHistoryProcessInstanceList(String processDefinitionId) {
        return historyService.createHistoryProcessInstanceQuery()
                .processDefinitionId(processDefinitionId)
                .orderAsc(HistoryProcessInstanceQuery.PROPERTY_STARTTIME)
                .list();
    }


    /***
     * 查询某一特定流程定义的所有历史活动实例
     *
     * @author
     * @date 2020/5/25 22:13
     * @param processDefinitionId 流程定义ID
     * @return java.util.List<org.jbpm.api.history.HistoryActivityInstance>
     */
    public List<HistoryActivityInstance> queryHistoryActivityInstanceList(String processDefinitionId) {
        return historyService.createHistoryActivityInstanceQuery()
                .processDefinitionId(processDefinitionId)
                .list();
    }

    /***
     * 获取指定流程中每个活动的平均执行时间
     *
     * @author
     * @date 2020/5/25 23:07
     * @param processDefinitionId 流程定义ID
     * @return java.util.Map<java.lang.String, java.lang.Number>
     */
    public Map<String, Number> getAvgDurationPerActivity(String processDefinitionId) {
        return historyService.avgDurationPerActivity(processDefinitionId);
    }


    /***
     * 获取指定活动定义每个转移的经过次数
     *
     * @author
     * @date 2020/5/25 23:18
     * @param processDefinitionId 流程定义ID
     * @param activityName  活动名称
     * @return java.util.Map<java.lang.String, java.lang.Number>
     */
    public Map<String, Number> getChoiceDistribution(String processDefinitionId, String activityName) {
        return historyService.choiceDistribution(processDefinitionId, activityName);
    }


}